/*
 * ADC.c
 *
 *  Created on: Jan 20, 2014
 *      Author: Rohit
 * This file contains the function when
 *
 */

#include "includes.h"


/*
 * Function: func_ADC
 *
 * Inputs:	 Mnemonic, Address_of_source, Address_of_destination
 * Outputs:  Result

Condition Codes: Z: set if result = 0; cleared otherwise
N: set if result < 0; cleared otherwise
c: set if (dst) was 177777 and (C) was 1; cleared otherwise
V: set if (dst) was 077777 and (C) was 1; cleared otherwise.

Description: Adds the contents of the C-bit into the destination. This permits
the carry from the addition of the two low-order words to be carried into the
high-order result.
*/

unsigned short func_ADC(unsigned short address_destination)
{
	unsigned short data_destination;
	int result_32_bit;
	unsigned short result;
	unsigned short carry;

	data_destination = read_from_memory(address_destination,DATA);

	carry =  get_C_flag();

	result_32_bit = carry + data_destination;
	result		  = (unsigned short)result_32_bit;

	write_in_memory(address_destination, result,DATA);

	update_Z_flag(result);
	update_N_flag(ADC, result);

	if((data_destination == 65535) && (carry == 1))
		set_C_flag();
	else
		clear_C_flag();

	if((data_destination == 32767) && carry == 1)
		set_V_flag();
	else
		clear_V_flag();

	return result;
}



